﻿using AiShua.CookiePool.Model;
using Dos.ORM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AiShua.CookiePool.DB
{

    public class DB
    {
        public static readonly DbSession Context = new DbSession(DatabaseType.SqlServer,"Server=192.168.0.107;Uid=sa;Pwd=Lxf641485146.;DataBase=AiShua");

        /// <summary>
        /// 添加Cookie
        /// </summary>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public bool AddCookie(T_Cookie cookie,out string msg)
        {
            msg = "";
            int row = 0;
            try
            {
                row = Context.Insert<T_Cookie>(cookie);
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
            }
            return row > 0 ? true : false;
        }

        /// <summary>
        /// 根据ID删除Cookie
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool DeleteCookie(string ID)
        {
            int row = 0;
            try
            {
                row = Context.Delete<T_Cookie>(ID);
            }
            catch (Exception ex)
            {
            }
            return row > 0 ? true : false;
        }

        /// <summary>
        /// 根据用户名删除Cookie
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool DeleteCookieByUserName(string userName)
        {
            int row = 0;
            try
            {
                var Cookie = Context.From<T_Cookie>().Where(t => t.UserName == userName).Select().ToFirstDefault();
                if (Cookie != null)
                {
                    row = Context.Delete<T_Cookie>(Cookie);
                }
            }
            catch (Exception ex)
            {
            }
            return row > 0 ? true : false;
        }

        /// <summary>
        /// 根据用户名获取Cookie
        /// </summary>
        /// <param name="username">用户名</param>
        /// <returns></returns>
        public T_Cookie GetCookieByUserName(string username,out string msg)
        {
            T_Cookie Cookie = null;
            msg = "";
            try
            {
                Cookie = Context.From<T_Cookie>().Where(t => t.UserName == username).ToFirstDefault();
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
            }
            return Cookie;
        }

        /// <summary>
        /// 根据ID获取Cookie
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public T_Cookie GetCookieById(string ID,out string msg)
        {
              T_Cookie Cookie = null;
            msg = "";
            try
            {
                Cookie = Context.From<T_Cookie>().Where(t => t.ID == ID).Select().ToFirstDefault();
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
            }
            return Cookie;
        }

        /// <summary>
        /// 获取所有Cookie
        /// </summary>
        /// <returns></returns>
        public List<T_Cookie> GetAllCookie(out string msg)
        {
            msg = "";
            List<T_Cookie> Cookies = new List<T_Cookie>();
            try
            {
                Cookies = Context.From<T_Cookie>().Select().ToList();
            }
            catch (Exception ex)
            {
                msg = ex.ToString();
            }
            return Cookies;
        }

        /// <summary>
        /// 获取所有Cookie的数量
        /// </summary>
        /// <returns></returns>
        public int GetCookieCount()
        {
            return Context.Count<T_Cookie>(new Where());
        }


    }
}